const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

// 库存明细
router.post('/stock/kcyj', async ctx => {
    let user = ctx.user
    let tenantId = user.tenantId;
    let formData = ctx.request.body.formData;
    let excel = ctx.request.body.excel;
    let invName = formData.invName?tool.xe.trim(formData.invName):'';
    let sql = `
        select a.tenantId, locationId, invId, qty, spec, unit1, locationName, invName, lowQty, highQty,qty-lowQty safeQty 
        from (
                 select a.tenantId,a.locationId,a.invId,round(isnull(a.lastCount,0) /spec,2) qty,b.spec,b.unit1,c.name locationName,b.name invName,isnull(b.lowQty,0) lowQty,isnull(b.highQty,0) highQty
                 from inventory a
                          inner join v_goods b on a.tenantId=b.tenantId and a.invId=b.id
                          inner join storage c on a.tenantId=c.tenantId and a.locationId=c.id and a.tenantId=${tenantId} 
                 and (b.name like '%${invName}%' or '${invName}'='')
             ) a order by locationId, qty-lowQty
        ;
    `
    let list = await db.query(sql, {type: QueryTypes.SELECT})
    if(excel) {
        return tool.doExcelOut(ctx, list)
    }
    return ctx.suc('',{
        total: 0,
        list
    })
})


module.exports = router;
